﻿using AntMovement.Core.Entity;
using AntMovernent.SolidTechESerialCard.Enum;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static AntMovernent.SolidTechESerialCard.SLDMV;

namespace AntMovernent.SolidTechESerialCard.Domain
{
    public class HardwareUpdateAxis : AntMovement.Core.IHardwareUpdate
    {
        private Axis _axis;

        private uint _nCardNo;
        private uint _axisNo;

        public HardwareUpdateAxis(uint nCardNo, uint axisNo, Axis axis)
        {
            _nCardNo = nCardNo;
            _axisNo = axisNo;
            _axis = axis;
        }

        public string Update()
        {
            uint valid = 0, el = 0;
            int error = SLDMV.SLDM_Get_Org(_axis.CardNo, _axis.AxisID, ref valid, ref el);
            if (error != 0) { return $"CardNo[{_axis.CardNo}] AxisNo[{_axis.AxisID}] SLDM_Get_Org ==>> {(SolidErrorCode)error}"; }
            _axis.Status.ORG = (el == 1);
            #region 限位
            error = SLDMV.SLDM_Get_NOT(_axis.CardNo, _axis.AxisID, ref valid, ref el);
            if (error != 0) { return $"CardNo[{_axis.CardNo}] AxisNo[{_axis.AxisID}] SLDM_Get_NOT ==>> {(SolidErrorCode)error}"; }
            _axis.Status.El_Right = (el == 1);

            error = SLDMV.SLDM_Get_POT(_axis.CardNo, _axis.AxisID, ref valid, ref el);
            if (error != 0) { return $"CardNo[{_axis.CardNo}] AxisNo[{_axis.AxisID}] SLDM_Get_POT ==>> {(SolidErrorCode)error}"; }
            _axis.Status.El_Left = (el == 1);

            error = SLDMV.SLDM_Get_Servo_Alarm(_axis.CardNo, _axis.AxisID, ref valid, ref el);
            if (error != 0) { return $"CardNo[{_axis.CardNo}] AxisNo[{_axis.AxisID}] SLDM_Get_POT ==>> {(SolidErrorCode)error}"; }
            _axis.Status.Alm = (el == 1);
            #endregion
            _axis.Status.EMG = false;
            int pos = 0;
            error = SLDMV.SLDM_Get_Pos(_axis.CardNo, _axis.AxisID, ref pos);
            if (error != 0) { return $"CardNo[{_axis.CardNo}] AxisNo[{_axis.AxisID}] SLDM_Get_Pos ==>> {(SolidErrorCode)error}"; }
            _axis.Status.CurrentPostion = pos;

            error = SLDMV.SLDM_Get_Enc(_axis.CardNo, _axis.AxisID, ref pos);
            if (error != 0) { return $"CardNo[{_axis.CardNo}] AxisNo[{_axis.AxisID}] SLDM_Get_Enc ==>> {(SolidErrorCode)error}"; }
            _axis.Status.EqPostion = pos;

            error = SLDMV.SLDM_Get_Reached(_axis.CardNo, _axis.AxisID, ref valid, ref el);
            if (error != 0) { return $"CardNo[{_axis.CardNo}] AxisNo[{_axis.AxisID}] SLDM_Get_Reached ==>> {(SolidErrorCode)error}"; }
            _axis.Status.INP = el == 1;

            error = SLDMV.SLDM_Get_Servo_Alarm(_axis.CardNo, _axis.AxisID, ref valid, ref el);
            if (error != 0) { return $"CardNo[{_axis.CardNo}] AxisNo[{_axis.AxisID}] SLDM_Get_Servo_Alarm ==>> {(SolidErrorCode)error}"; }
            _axis.Status.EMG = el == 1;

            error = SLDMV.SLDM_Get_Enc_Z(_axis.CardNo, _axis.AxisID, ref valid, ref el);
            if (error != 0) { return $"CardNo[{_axis.CardNo}] AxisNo[{_axis.AxisID}] SLDM_Get_Enc_Z ==>> {(SolidErrorCode)error}"; }
            _axis.Status.EZ = el == 1;

            uint running = 0;
            error = SLDMV.SLDM_Get_Running(_axis.CardNo, _axis.AxisID, ref running);
            if (error != 0) { return $"CardNo[{_axis.CardNo}] AxisNo[{_axis.AxisID}] SLDM_Get_Running ==>> {(SolidErrorCode)error}"; }
            _axis.Status.SportStatus = running == 1;

            uint homing = 0, homed = 0;
            error = SLDMV.SLDM_Get_Homing(_axis.CardNo, _axis.AxisID, ref homing, ref homed);
            if (error != 0) { return $"CardNo[{_axis.CardNo}] AxisNo[{_axis.AxisID}] SLDM_Get_Homing ==>> {(SolidErrorCode)error}"; }
            _axis.Status.Homing = homing == 0 && homed == 1;
            double pe = 0;
            if (_axis.UnitConversion.EnableGearbox)
            {
                if (_axis.UnitConversion.EnableRotationMode)
                {
                    //旋转模式

                }
                else
                {
                    //线性模式
                    pe = (_axis.UnitConversion.WeeklyItinerary * _axis.UnitConversion.MotorGearRatio) / (_axis.UnitConversion.CyclePulse * _axis.UnitConversion.WorkingGearRatio);
                }
            }
            else
            {
                pe = _axis.UnitConversion.WeeklyItinerary / _axis.UnitConversion.CyclePulse;
            }
            _axis.UnitConversion.ActualPosition = pe * (double)_axis.Status.CurrentPostion;
            return "ok";
        }
    }
}
